package com.example.a79273.shandianban.fragment;

import android.content.Intent;
import android.os.Bundle;
import java.lang.reflect.Type;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.shandianban.ProductInfoActivity;
import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.adapter.YouHuoRecyclerAdapter;
import com.example.a79273.shandianban.model.YouHuoBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by lenovo on 2016/8/29.
 */
public class YouHuoSonFragment extends Fragment {
    private View view;
    private int count;
    private String path;
    private TextView mTextViewTitle;
    private RequestParams mParams;
    private RecyclerView mRecyclerView;
    private List<YouHuoBean.DataBean.ListBean> mData;
    private YouHuoRecyclerAdapter mAdapter;
    private StaggeredGridLayoutManager mManager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_youhuoson,null);
        initView();
        initData();
        setData();
        setListener();
        return view;
    }

    //RecyclerView的点击事件
    private void setListener() {
        mAdapter.setOnItemClickListener(new YouHuoRecyclerAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), ProductInfoActivity.class);
                intent.putExtra("proid",mData.get(position).getProid());
               startActivity(intent);
            }
        });
    }

    private void setData() {
        //设置每一个页面的标题
        String title = null;
        switch (count) {
            case  0:
                title = "最新";
                break;
            case 1:
                title = "最热";
                break;
            case 2:
                title = "女装";
                break;
            case 3:
                title = "推荐";
                break;
        }
        mTextViewTitle.setText(title);
        mRecyclerView.setLayoutManager(mManager);
        mRecyclerView.setAdapter(mAdapter);

        
        
    }

    private void initData() {
        //获取前一个页面传过来的值
        Bundle bundle = getArguments();
        count = bundle.getInt("count");
        path = bundle.getString("path");
        mData = new ArrayList<>();
        mAdapter = new YouHuoRecyclerAdapter(mData,getActivity());
        mManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        //------------------获取数据------------------
        mParams = new RequestParams(path);
        x.http().get(mParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(!TextUtils.isEmpty(result)) {
                    Type listType = new TypeToken<LinkedList<YouHuoBean>>(){}.getType();
                    Gson gson = new Gson();
                    LinkedList<YouHuoBean> resources = gson.fromJson(result, listType);
                    for (int i = 0; i < resources.size(); i++) {
                        List<YouHuoBean.DataBean.ListBean> list = resources.get(i).getData().getList();
                        //Log.i("TAG","---"+list.size()+list.toString());
                        mData.addAll(list);
                    }
                    mAdapter.notifyDataSetChanged();
                    /*for (Iterator iterator = resources.iterator(); iterator.hasNext();) {
                        YouHuoBean resource = (YouHuoBean) iterator.next();
                        List<YouHuoBean.DataBean.ListBean> list = resource.getData().getList();

                    }*/
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网络出小差啦...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initView() {
        mTextViewTitle = (TextView) view.findViewById(R.id.tv_youhuoson_title);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_youhuoson_content);
    }
}
